package oa.hleast.xswl.service.activity.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.mapper.activity.CmsActivityCalendarMapper;
import oa.hleast.xswl.mapper.activity.CmsCommonActivityMapper;
import oa.hleast.xswl.mapper.activity.CmsUserSignUpMapper;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityCalendar;
import oa.hleast.xswl.pojo.domain.activity.CmsCommonActivity;
import oa.hleast.xswl.pojo.domain.activity.CmsUserSignUp;
import oa.hleast.xswl.pojo.form.activity.CmsActivityCalendarForm;
import oa.hleast.xswl.pojo.vo.activity.CmsActivityCalendarVo;
import oa.hleast.xswl.pojo.vo.activity.CmsActivityCountVo;
import oa.hleast.xswl.service.activity.ICmsActivityCalendarService;
import oa.hleast.xswl.service.activity.ICmsUserSignInService;
import oa.hleast.xswl.service.activity.ICmsUserSignUpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
@AllArgsConstructor
public class CmsActivityCalendarServiceImpl extends ServiceImpl<CmsActivityCalendarMapper, CmsActivityCalendar> implements ICmsActivityCalendarService {
    private final CmsUserSignUpMapper signUpMapper;
    private final ICmsUserSignUpService signUpService;
    private final ICmsUserSignInService signInService;
    private final CmsCommonActivityMapper commonActivityMapper;

    @Override
    public Boolean addList(List<CmsActivityCalendarForm> formList) {

        List<CmsActivityCalendar> calendarList = formList.stream().map(this::conversionToPojo).collect(Collectors.toList());
        boolean save = this.saveBatch(calendarList);
        log.info("save = {}", save);
        return save;
    }


    @Override
    public boolean add(CmsActivityCalendarForm form) {

        CmsActivityCalendar pojo = conversionToPojo(form);
        log.info("pojo = {}", pojo);
        return this.save(pojo);
    }

    @Override
    public boolean update(CmsActivityCalendar form) {


        return this.updateById(form);
    }

    @Override
    public Page<CmsActivityCountVo> signInPageList(Integer page, Integer limit, Long activityId, Integer display) {
        CmsCommonActivity commonActivity = commonActivityMapper.selectById(activityId);
        String title = commonActivity.getTitle();

        Page<CmsActivityCalendar> pojoPage = this.page(new Page<>(page, limit), new LambdaQueryWrapper<CmsActivityCalendar>()
                .eq(CmsActivityCalendar::getActivityId, activityId)
                .orderByAsc(CmsActivityCalendar::getYear)
                .orderByAsc(CmsActivityCalendar::getMonth)
                .orderByAsc(CmsActivityCalendar::getDay)
                .orderByDesc(CmsActivityCalendar::getGmtCreate)
                .orderByDesc(CmsActivityCalendar::getGmtModified)
        );

        if (pojoPage.getTotal() == 0) {
            return new Page<>(page, limit, 0);
        }


        List<CmsActivityCountVo> voList = pojoPage.getRecords().stream().map(pojo -> {

            CmsActivityCountVo vo = conversionToCountVo(pojo);
            // 添加报名人数
            vo.setSignUpNum(signUpService.countSignUpByCalendar(pojo.getId()));
            // 添加签到人数
            vo.setSignInNum(signInService.countSignInByCalendar(pojo.getId()));
            vo.setActivityTitle(title);
            return vo;
        }).collect(Collectors.toList());
        Page<CmsActivityCountVo> voPage = new Page<>(page, limit, pojoPage.getTotal());
        voPage.setRecords(voList);

        return voPage;
    }

    private CmsActivityCountVo conversionToCountVo(CmsActivityCalendar pojo) {
        CmsActivityCountVo vo = new CmsActivityCountVo();
        BeanUtil.copyProperties(pojo, vo);

        vo.setCalendarId(pojo.getId());
        return vo;
    }

    private CmsActivityCalendarVo conversionToVo(CmsActivityCalendar pojo) {
        CmsActivityCalendarVo vo = new CmsActivityCalendarVo();
        BeanUtil.copyProperties(pojo, vo);
        return vo;
    }

    @Override
    public boolean deleteById(Long id) {
        List<CmsUserSignUp> userSignUpList = signUpMapper.selectList(new LambdaQueryWrapper<CmsUserSignUp>()
                .eq(CmsUserSignUp::getCalendarId, id)
        );
        if (userSignUpList.size() > 0) {
            throw new BizException("该场次已经有人报名，您不能删除");
        }

        return this.removeById(id);
    }

    @Override
    public boolean deleteByIds(List<String> asList) {
        return this.removeByIds(asList);
    }

    @Override
    public CmsActivityCalendar detail(Long id) {
        return this.getById(id);
    }

    private CmsActivityCalendar conversionToPojo(CmsActivityCalendarForm form) {
        CmsActivityCalendar pojo = new CmsActivityCalendar();
        BeanUtil.copyProperties(form, pojo);
        return pojo;
    }

}
